from django.shortcuts import *
from .models import *


# Create your views here.
def index(request):
    return render(request, 'index.html')


def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        uname = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        user = User.objects.values('uname').filter(uname=uname, pwd=pwd)
        if user:
            for i in user:
                n = i['uname']
                request.session['name'] =n
            return redirect(reverse('all'))
        else:
            return HttpResponse('hahha')


def all(request):
    if request.method == 'GET':
        all = Student.objects.all()
        return render(request, 'all.html', {'alls': all})
    else:
        ss = request.POST.get('ss')
        b = Student.objects.filter(sname__contains=ss)
        return render(request, 'all.html', {'ss': b})

def add(request):
    if request.method == 'GET':
        return render(request, 'add.html')
    else:
        sname = request.POST.get('sname')
        age = request.POST.get('age')
        ns = request.POST.get('ns')
        gjz = request.POST.getlist('gjz')
        gjz1 = [str(i) for i in gjz]
        gjz2 = ','.join(gjz1)
        fenlei = request.POST.get('fenlei')
        new = Student(sname=sname, age=age, ns=ns, gjz=gjz2, fenlei=fenlei)
        new.save()
        return redirect(reverse('all'))


def delete(request, id):
    st = Student.objects.filter(pk=id)
    st.delete()
    return redirect(reverse('all'))


def update(request):
    return None